#!/bin/sh

# Copyright (C) 2000-2006 Free Software Foundation, Inc.
#
# Authors: Peter Gerwinski <peter@gerwinski.de>
#          Frank Heckenbach <frank@pascal.gnu.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Pascal is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Pascal; see the file COPYING. If not, write to the
# Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

if [ x"$1" = x"-h" ] || [ x"$1" = x"--help" ]; then
  cat << EOF
Usage: `basename "$0"` [options] [file...]
Options (must be given in this order!):
  -h, --help  Display this help and exit
  -p          Output progress messages
  -n          Don't make extraclean after testing
  -c          Don't test, just make extraclean
  -t          Make pascal.check-long rather than pascal.check-short
  -l          Make pascal.check-long-nolog rather than pascal.check-short
  -g          Use GP
EOF
  exit
fi

# Avoid a flood of error messages if accidentally run from a removed
# directory
pwd > /dev/null || exit 1

if [ x"$GPC_SRCDIR" = x ]; then
  echo "GPC_SRCDIR must be set" >&2
  exit 1
fi

if [ -e "$GPC_SRCDIR/gcc/p/gpc.c" ]; then
  srcdir="$GPC_SRCDIR/gcc/p"
elif [ -e "$GPC_SRCDIR/p/gpc.c" ]; then
  srcdir="$GPC_SRCDIR/p"
else
  echo "Invalid GPC_SRCDIR" >&2
  exit 1
fi

TESTDIR="$srcdir/test"

DJGPP_NO_OUT=""; export DJGPP_NO_OUT

TARGET1="pascal.check-short"
TARGET2="extraclean"
TEST_RUN_FLAGS=""
VARS=""

while true; do
  case "$1" in
    -p) TEST_RUN_FLAGS="$TEST_RUN_FLAGS -p"; shift;;
    -n) TARGET2="nop"; shift;;
    -c) TARGET1="nop"; shift;;
    -t) TARGET1="pascal.check-long"; shift;;
    -l) TARGET1="pascal.check-long-nolog"; shift;;
    -g) VARS="$VARS GP=gp"; shift;;
    -*) echo "`basename "$0"`: invalid option \`$1'" >&2; exit 1;;
    *)  break;;
  esac
done

if [ x"$TARGET1" != x"nop" ]; then
  if [ $# -ge 1 ]; then
    make -s -f "$TESTDIR/Makefile" srcdir="$TESTDIR" TEST_RUN_FLAGS="$TEST_RUN_FLAGS" $VARS $TARGET1 "MASK=$*"
  else
    make -s -f "$TESTDIR/Makefile" srcdir="$TESTDIR" TEST_RUN_FLAGS="$TEST_RUN_FLAGS" $VARS $TARGET1
  fi
fi

if [ x"$TARGET2" != x"nop" ]; then
  make -s -f "$TESTDIR/Makefile" srcdir="$TESTDIR" TEST_RUN_FLAGS="$TEST_RUN_FLAGS" $TARGET2
fi
